<?php

/**
 * 微信投票用户 Service
 * 
 * @path Apps/Common/Service/WxEventVoteUserService.class.php
 * @author Zix <zix2002@gmail.com> 
 * @version v1.0 , 2015-09-11
 */

namespace Common\Service;

class WxEventVoteUserService {

  private static $instance;
  var $status = array(
    0 => '待审核' ,
    1 => '审核通过',
  );
  
  public static function instance() {
    if (self::$instance == null) {
      $instance = new WxEventVoteUserService();
    }

    return $instance;
  }

  function get_default_row() {
    return array(
      'user_id' => '',
      'ipaddress' => '',
      'create_time' => date('Y-m-d H:i:s'),
      'award_id' => '',
      'award_sn' => '',
      'award_status' => '0',
      'award_time' => '',
      'award_take_time' => '',
    );
  }

  function get_by_id($id) {
    $WxEventVoteUser = M('WxEventVoteUser');
    $data = $WxEventVoteUser->find($id);
    return $data ? $data : array();
  }

  function get_by_cond($config) {
    $default = array(
      'event_id' => '' ,
      'page' => 1,
      'page_size' => 10 ,
      'status' => '',
      'count' => FALSE,
      'order' => 'DESC',
      'sort' => 'id',
    );

    $config = extend($config, $default);

    $WxEventVoteUser = M('WxEventVoteUser');

    $where = array();

    if (!empty($config['status'])) {
      $where['evu.status'] = $config['status'];
    }
    
    if (!empty($config['event_id'])) {
      $where['evu.event_id'] = $config['event_id'];
    }

    if ($config['count']) {
      return $WxEventVoteUser->alias('evu')->where($where)->count();
    } else {
      $order = 'evu.'.$config['sort'] . ' ' . $config['order'];
      $limit = ($config['page'] - 1 ) * $config['page_size'] . ' , ' . $config['page_size'];
      $data = $WxEventVoteUser
        ->alias('evu')
        ->field('evu.* , u.nickname , a.name as award_name ')
        ->join('mer_user u on u.id = evu.user_id' , 'left')
        ->join('wx_event_vote_award a on a.id = evu.award_id' , 'left')
        ->where($where)
        ->limit($limit)->order($order)->select();
      //echo $WxEventVoteUser->_sql();
      return $data ? $data : array();
    }
  }

  function create($data) {
    $WxEventVoteUser = M('WxEventVoteUser');
    $ret = $WxEventVoteUser->add($data);
    if ($ret === FALSE) {
      return ajax_arr('添加失败', FALSE);
    } else {
      return ajax_arr('添加成功', TRUE, array(
        'id' => $ret
      ));
    }
  }

  function update($id, $data) {
    $WxEventVoteUser = M('WxEventVoteUser');

    $ret = $WxEventVoteUser->where("id = %d", $id)->save($data);
    if ($ret === FALSE) {
      return ajax_arr('编辑失败', FALSE);
    } else {
      return ajax_arr('编辑成功', TRUE);
    }
  }

  function delete($ids) {
    $WxEventVoteUser = M('WxEventVoteUser');
    $ret = $WxEventVoteUser->delete($ids);
    if ($ret == 0) {
      return ajax_arr('未删除任何数据', FALSE);
    } else if (!$ret) {
      return ajax_arr('删除失败', FALSE);
    } else {
      return ajax_arr('删除' . $ret . '行数据', TRUE);
    }
  }
  
  function count_daily_by_user( $user_id ) {
    $WxEventVoteUser = M('WxEventVoteUser');
    
    $date = date('Y-m-d');
    $where = array(
      '_string' => "DATE_FORMAT(create_time , '%Y-%m-%d') = '$date' " ,
      'user_id' => $user_id ,
    );
    $count = $WxEventVoteUser->where( $where )->count('id'); 
    //echo $WxEventVoteUser->_sql();
    return $count ;
  }
  
  function get_by_user_player_today( $user_id , $player_id ) {
    $WxEventVoteUser = M('WxEventVoteUser');
    
    $date = date('Y-m-d');
    $where = array(
      '_string' => "DATE_FORMAT(create_time , '%Y-%m-%d' ) = '$date' " ,
      'user_id' => $user_id ,
      'player_id' => $player_id 
    );
    $data = $WxEventVoteUser->where( $where )->find(); 
    
    return $data ? $data : array();
  }

}
